package cn.com.blueInfo.bpm.number.service.impl;

import cn.com.blueInfo.bpm.number.convert.NumberRuleConvert;
import cn.com.blueInfo.bpm.number.entity.NumberRule;
import cn.com.blueInfo.bpm.number.repository.NumberRuleRepository;
import cn.com.blueInfo.bpm.number.request.NumberRuleDTO;
import cn.com.blueInfo.bpm.number.response.NumberRuleVO;
import cn.com.blueInfo.bpm.number.service.NumberRuleService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 编号规则定义表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:26:29
 */
@Log4j2
@Service
public class NumberRuleServiceImpl implements NumberRuleService {

    @Autowired
    private NumberRuleRepository numberRuleRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private NumberRuleConvert numberRuleConvert;

    /**
     * 新增编号规则定义
     * @param numberRuleDTO 编号规则定义参数
     */
    @Override
    public ResultInfo<Integer> addNumberRule(NumberRuleDTO numberRuleDTO) {
        NumberRule numberRule = numberRuleConvert.dtoToEntity(numberRuleDTO);
        numberRule.setId(snowflakeIdGenerator.nextId());
        Integer addSize = numberRuleRepository.createNumberRule(numberRule);
        return ResultInfo.<Integer>builder().data(addSize).message("编号规则定义成功新增 {} 条").build();
    }

    /**
     * 批量新增编号规则定义
     * @param numberRuleDTOList 编号规则定义参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddNumberRule(List<NumberRuleDTO> numberRuleDTOList) {
        List<NumberRule> numberRuleList = numberRuleConvert.dtoToEntity(numberRuleDTOList);
        for (NumberRule numberRule : numberRuleList) numberRule.setId(snowflakeIdGenerator.nextId());
        Integer addSize = numberRuleRepository.batchCreateNumberRule(numberRuleList);
        return ResultInfo.<Integer>builder().data(addSize).message("编号规则定义成功新增 {} 条").build();
    }

    /**
     * 删除编号规则定义
     * @param numberRuleId 编号规则定义ID
     */
    @Override
    public ResultInfo<Integer> removeNumberRule(Long numberRuleId) {
        Integer removeSize = numberRuleRepository.deleteNumberRule(numberRuleId);
        return ResultInfo.<Integer>builder().data(removeSize).message("编号规则定义成功删除 {} 条").build();
    }

    /**
     * 批量删除编号规则定义
     * @param numberRuleIdList 编号规则定义ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveNumberRule(List<Long> numberRuleIdList) {
        Integer removeSize = numberRuleRepository.batchDeleteNumberRule(numberRuleIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("编号规则定义成功删除 {} 条").build();
    }

    /**
     * 更新部分编号规则定义
     * @param numberRuleUpdatePartialDTO 编号规则定义参数
     */
    @Override
    public ResultInfo<Integer> modifyNumberRulePartial(NumberRuleDTO.UpdatePartialDTO numberRuleUpdatePartialDTO) {
        NumberRule numberRule = numberRuleConvert.dtoToEntity(numberRuleUpdatePartialDTO);
        Integer modifySize = numberRuleRepository.updateNumberRulePartial(numberRule);
        return ResultInfo.<Integer>builder().data(modifySize).message("编号规则定义成功更新 {} 条").build();
    }

    /**
     * 更新全部编号规则定义
     * @param numberRuleDTO 编号规则定义参数
     */
    @Override
    public ResultInfo<Integer> modifyNumberRule(NumberRuleDTO numberRuleDTO) {
        NumberRule numberRule = numberRuleConvert.dtoToEntity(numberRuleDTO);
        Integer modifySize = numberRuleRepository.updateNumberRule(numberRule);
        return ResultInfo.<Integer>builder().data(modifySize).message("编号规则定义成功更新 {} 条").build();
    }

    /**
     * 查询一条编号规则定义
     * @param numberRuleId 编号规则定义ID
     */
    @Override
    public ResultInfo<NumberRuleVO> queryNumberRuleById(Long numberRuleId) {
        NumberRule numberRule = numberRuleRepository.getNumberRuleById(numberRuleId);
        NumberRuleVO numberRuleVO = numberRuleConvert.entityToVO(numberRule);
        return ResultInfo.<NumberRuleVO>builder().data(numberRuleVO).message("编号规则定义查询成功").build();
    }

    /**
     * 分页查询编号规则定义
     * @param numberRuleQueryDTO 编号规则定义参数
     */
    @Override
    public ResultInfo<Page<NumberRuleVO>> queryNumberRulePage(NumberRuleDTO.QueryDTO numberRuleQueryDTO) {
        Page<NumberRule> numberRulePage = numberRuleRepository.getNumberRulePage(numberRuleQueryDTO);
        Page<NumberRuleVO> resultData = numberRuleConvert.convertPage(numberRulePage);
        return ResultInfo.<Page<NumberRuleVO>>builder()
                .data(resultData)
                .message("编号规则定义分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 编号规则定义 数量
     * @param numberRuleQueryDTO 编号规则定义参数
     */
    @Override
    public ResultInfo<Long> queryNumberRuleCount(NumberRuleDTO.QueryDTO numberRuleQueryDTO) {
        Long resultCount = numberRuleRepository.getNumberRuleCount(numberRuleQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("编号规则定义数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 编号规则定义
     * @param numberRuleQueryDTO 编号规则定义参数
     */
    @Override
    public ResultInfo<List<NumberRuleVO>> queryNumberRuleList(NumberRuleDTO.QueryDTO numberRuleQueryDTO) {
        List<NumberRule> numberRuleList = numberRuleRepository.getNumberRuleList(numberRuleQueryDTO);
        List<NumberRuleVO> resultData = numberRuleConvert.entityListToVoList(numberRuleList);
        return ResultInfo.<List<NumberRuleVO>>builder()
                .data(resultData)
                .message("编号规则定义分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
